package com.heima.apis.article.fallback;

import com.heima.apis.article.IArticleClient;
import com.heima.model.article.dtos.ArticleCommentDto;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.comment.dtos.CommentConfigDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.StatisticsDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * feign失败配置(降级逻辑)
 *
 */
//步骤一：feign 远程调用失败的回调，远程调用服务降级处理
@Component
@Slf4j
public class IArticleClientFallback implements IArticleClient {
// 步骤三：在自媒体微服务中添加类InitConfig，扫描降级代码类的包
// 客户端heima-leadnews-wemedia(调用远程服务的那一端，服务端是heima-leadnews-article，是被调用的那端)开启降级，
// 去到application.yml配置文件中配置feign: hystrix: enabled: true
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {//  503 服务器内部错误
        log.error("熔断降级---远程调用saveArticle失败，参数：{}",dto);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取数据失败");
    }

    @Override
    public ResponseResult queryLikesAndConllections(Integer id, Date beginDate, Date endDate) {
        log.error("熔断降级---远程调用queryLikesAndConllections失败，参数：{},{},{}",id,beginDate,endDate);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取数据失败");
    }

    @Override
    public PageResponseResult newPage(StatisticsDto dto) {
        log.error("熔断降级---远程调用newPage失败，参数：{}",dto);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),0);
        responseResult.setCode(501);
        responseResult.setErrorMessage("获取数据失败");
        return responseResult;
    }

    @Override
    public ResponseResult findArticleConfigByArticleId(Long articleId) {
        log.error("熔断降级---远程调用findArticleConfigByArticleId失败，参数：{}",articleId);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取数据失败");
    }

    @Override
    public PageResponseResult findNewsComments(ArticleCommentDto dto) {
        log.error("熔断降级---远程调用findNewsComments失败，参数：{}",dto);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),0);
        responseResult.setCode(501);
        responseResult.setErrorMessage("获取数据失败");
        return responseResult;
    }

    @Override
    public ResponseResult updateCommentStatus(CommentConfigDto dto) {
        log.error("熔断降级---远程调用updateCommentStatus失败，参数：{}",dto);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取数据失败");
    }
}
